<?php
/**
 * 角色类
 */

##@include_once CLS_PTH.'system/Manager.class.php';
##@include_once CLS_PTH.'admin/SubRole.class.php';

class Role extends MyBase{
	private $menu		= null;
	//private $oManager	= null;
	//private $oSubRole	= null;
	
	function __construct(){
		parent::__construct('role.id');
		
		$this->menus = array();
		//$this->oManager = new Manager();
		//$this->oSubRole = new SubRole();
	}
	
	/**
	 * 修改角色
	 */
	public function edit($post){
		$id = empty($post['id']) ? 0 : intval($post['id']);
		$select = array('rolename' => $post['rolename'],);
		if($id > 0){
			$select['kept'] = " AND id!={$id}";
		}
		$rs = $this->db->get_row($this->tbname, $select, 'id');
		if(!empty($rs)) {
			return lang('already').lang('exist');
		}
		$post['menus']	= isset($post['menus']) && is_array($post['menus']) ? $post['menus'] : array();
		$post['powers']	= isset($post['powers']) && is_array($post['powers']) ? $post['powers'] : array();
		$post['access']	= isset($post['access']) && is_array($post['access']) ? $post['access'] : array();
		$post['setting']= isset($post['setting']) && is_array($post['setting']) ? $post['setting'] : array();
		$data = array(
			'rolename'	=> $post['rolename'],
			'menus'		=> serialize($post['menus']), 
			'powers'	=> serialize($post['powers']),
			'access'	=> serialize($post['access']),
			'subset'	=> 'DEFAULT',
			'level'		=> intval($post['level']), 
			'setting'	=> serialize($post['setting']),
			'rank'		=> $post['rank'],
			'memo'		=> $post['memo'],
		);
		if($id > 0){
			$data['updatetime'] = TIME;
			$r = $this->db->update($this->tbname, array('id' => $id), $data);
		}else{
			$data['addtime'] = TIME;
			$r = $id = $this->db->insert($this->tbname, $data);
		}
		if($r > 0){
			$this->cache_role($id, $data, $post['menus'], $post['powers'], $post['access'], $post['setting']);	// 缓存角色
		}
		return $r;		
	}
			
	/**
	 * 删除角色
	 *
	 * @param int			$id
	 * @return int
	 */
	public function del($post){
		$iRtn = 0;
		if(!is_array($post['list']))$post['list'] = array($post['list']);
		foreach($post['list'] as $id){
			
			$r = $this->db->delete($this->tbname, array('id' => $id, 'kept' => " AND id<>1"));
			if($r > 0){
				$iRtn ++;
				$this->cache_del($id);
			
				// 删除隶属管理员
				$oManager = new Manager();
				$list = $oManager->get_all('id', array('role' => $id), array());
				if(!empty($list)){
					foreach($list['list'] as $row){
						$oManager->del($row['id']);
					}
				}
			}
		}
		return $iRtn;
	}
	
	/**
	 * 取一条记录
	 *
	 * @param int			$id
	 * @param string		$fields
	 * @return array
	 */
	/*public function get_row($id, $fields = '*'){
		$select = array('id' => $id);
		return $this->db->get_row($this->tbname, $select, $fields);
	}*/
	
	/**
	 * 取列表
	 *
	 * @param string		$fields
	 * @param array			$select
	 * @param int			$page
	 * @param int			$size
	 * @return array
	 */
	public function get_list($fields = '*', $select = array(), $order = array(), $page = 1, $size = 10, $offset = PAGE_OFFSET){
		$managerinfo = & VS::get('managerinfo');
		$select['subset'] = empty($select['subset']) ? 'DEFAULT' : trim($select['subset']);
		if(!empty($select['keyword'])){
			$select['kept'] .= " AND `rolename` like '%{$select['keyword']}%'";
		}
		return $this->db->get_list($this->tbname, $fields, $select, $order, $page, $size, $offset);
	}
	
	public function getAllRoles($fields = '*'){
		$sql = "SELECT $fields FROM `{$this->tbname}` WHERE `subset`='DEFAULT' ORDER BY `rank` DESC,`id` DESC";
		return $this->db->sql_all($sql);
	}
	
	/********************************************************************************
	 * 缓存
	 ********************************************************************************/
	
	private function cache_role($roleid, $roleinfo, $menus, $powers, $access = array(), $setting){
		$menus_cache = cache_read('menus.php');
		if(empty($access)){	// 如果无后台权限，先将后台管理菜单去除
			foreach($menus_cache['menu_tree'] as $key => $menu){
				if($menu['path'] == 'admin_access'){
					unset($menus_cache['menu_tree'][$key]);
				}
			}
		}
		
		$oMenu = new Menu();
		$menu_tree = $oMenu->treval_menu($menus_cache['menu_tree'], $menus);
		$menu_list = $oMenu->menu_tree_to_list($menu_tree, 0);
		
		$menu_powers = array();
		foreach($menu_list as $menu) {
			$menu_powers[$menu['id']] = trim(array_pop(explode('?', $menu['path'])));
		}
		if(!empty($access)){	// 如果有后台管理权限
			$submenu_tree = array();
			foreach($menu_tree as $menu){
				if(in_array($menu['path'], $access)){
					$submenu_tree[] = $menu;
				}
			}
			$submenu_list = $oMenu->menu_tree_to_list($submenu_tree, 0);
			cache_write('menu_'.$roleid.'.php', array('menu_tree' => $submenu_tree, 'menu_list' => $submenu_list));
		}
		$subset_arr = array();
		foreach(Statics::$access_arr as $key => $val){
			if(in_array($key, $access)){
				$subset_arr[$key] = $val;
			}
		}
		foreach($powers as $key => $val) {
			$powers[$val] = true;
			unset($powers[$key]);
		}
		$roleinfo['roleid'] = $roleid;
		return cache_write('role_'.$roleid.'.php', array('info' => $roleinfo, 'menu_tree' => $menu_tree, 'menu_powers' => $menu_powers, 'powers' => $powers, 'subset' => $subset_arr, 'setting' => $setting));
	}
	
	/********************************************************************************
	 * 缓存
	 ********************************************************************************/
	
	public function cache($post){
		if(empty($post['list'])){
			$ids = $post['lst'];
		}else{
			$ids = $post['list'];
		}
		return $this->cache_all($ids);
	}
	
	public function cache_all($ids = null){
		if(!isset($ids) || empty($ids)) {
			$ids = array();
			$roles = $this->getAllRoles('id');
			if(!empty($roles['list'])){
				foreach($roles['list'] as $row){
					$ids[] = $row['id'];
				}
			}
		}
		
		$result = 0;
		foreach($ids as $key => $id) {
			$roleinfo = $this->get_row($id);
			if(!empty($roleinfo)){
				$access = empty($roleinfo['access']) ? array() : unserialize($roleinfo['access']);
				$setting = empty($roleinfo['setting']) ? array() : unserialize($roleinfo['setting']);
				$r = $this->cache_role($id, $roleinfo, unserialize($roleinfo['menus']), unserialize($roleinfo['powers']), $access, $setting);
				if($r > 0){
					$result ++;
				}
			}
		}
		return $result;
	}
	
	private function cache_del($ids){
		if(!is_array($ids)){
			$ids = array($ids);
		}
		foreach($ids as $id){
			cache_delete('role_'.$id.'.php');
		}
		return true;
	}
	
	
}
?>